<?
class UserOnline{
     private $utoday,$lastday,$lastweek,$lastmonth,$lastyear;
	 
     public function __construct() {	 
	    $this->utoday = time();
		$this->setLastday();
	    $this->setLastweek();
		$this->setLastmonth();
	    $this->setLastYear();
	 }
	 
	 public function setLastday(){
	    $this->lastday = date( "Y-m-d", mktime (0,0,0,date("m",$this->utoday), 
		                                date("d",$this->utoday)-1,  date("Y",$this->utoday)) ); 
	 } 
	 
	 public function setLastweek(){
	    $this->lastweek = date( "Y-m-d", mktime (0,0,0,date("m",$this->utoday),
		                                  date("d",$this->utoday)-7,  date("Y",$this->utoday)) );
	 }
	 
	 public function setLastmonth(){
	    $this->lastmonth = date( "m-Y", mktime (0,0,0,date("m",$this->utoday)-1, 
		                                    date("d",$this->utoday),  date("Y",$this->utoday)) );
	 }
	 
	 public function setLastYear(){
	    $this->lastyear = date( "Y", mktime (0,0,0,date("m",$this->utoday), 
		                                date("d",$this->utoday),  date("Y",$this->utoday)-1) );
	 }
	 
	 public function addCounter(){
	    $today = date( "Y-m-d", $this->utoday );
	    $sql_check =  "select counter from tb_counter where date_visit = '$today' ";
        $result_check = mysql_query($sql_check)
		                        or die("$sql_check <br>".mysql_error());

        $rows = mysql_num_rows($result_check);
    
	    if($rows==0){ //ถ้ายังไม่มีข้อมูลของวันนี้
		   $result= mysql_query("insert into tb_counter values('$today','1') ");
		   $counter = 1;
	    }else{ 
		   $result1= mysql_fetch_array($result_check);
		   $counter=$result1[counter];
		   /* Update Counter +1 */
		   $counter_new=$counter+1;
		   if(empty($_SESSION['user_usepage'])){
			  $sql2="update tb_counter set counter='$counter_new' 
			             where date_visit = '$today' ";
			  $dbquery2=mysql_query($sql2);
			  $_SESSION['user_usepage'] = session_id();
		   }	
	    }
		$this->count_today = number_format($counter,'0','.',',');
	 } // function
	 
	 public function getCount_Today(){
	    return $this->count_today;
	 }
	 
	 public function getCount_Yesterday(){
	    $result_lastday = mysql_query("select counter from tb_counter 
	                        where date_visit =  '".$this->lastday."' ")or die(mysql_error());
        $fetch_lastday = mysql_fetch_array($result_lastday);
        return  number_format($fetch_lastday[counter],'0','.',',');
	 }
	 
	 public function getCount_LastWeek(){
	    $sql = "select sum(counter) as counter from tb_counter 
		           where date_visit between '".$this->lastweek."' and '".$this->lastday."' ";
        $result_lastweek = mysql_query($sql)or die(mysql_query());
        $fetch_lastweek = mysql_fetch_array($result_lastweek);
	    return number_format($fetch_lastweek[counter],'0','.',',');
	 }
	 
	 public function getCount_All(){
	    $sql = "select sum(counter) as counter from tb_counter ";
        $result = mysql_query($sql)or die(mysql_query());
        $fetch = mysql_fetch_array($result);
	    return number_format($fetch[counter],'0','.',',');
	 }  
	 
	 public function getUseronline($user){
	    $timeoutseconds 	= 300 ; 
        $visitor = $user;
        $timestamp = time();                                          
        $timeout = $timestamp - $timeoutseconds;  

        mysql_query( "INSERT INTO useronline (timestamp, visitor) 
                      VALUES  ( '$timestamp','$visitor' )")or die(mysql_error()); 

        mysql_query("DELETE FROM useronline WHERE timestamp < $timeout") 
                                or die(mysql_error()); 

         $result = mysql_query("SELECT DISTINCT  visitor  FROM useronline") 
		                 or die(mysql_error()); 

         return  mysql_num_rows($result);
	 }
}
?>